/**   
 * @Title: TitleMenu.java 
 * @Package com.esun.rabbit2.viewer.mainact.hemaicenteragp 
 * @Description: 顶部菜单 
 * @author: City Jiangsy@500wan.com   
 * @date: 2011-11-29 上午10:09:19  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.titlemenu;

import java.util.ArrayList;
import java.util.Hashtable;

import com.esun.ui.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @ClassName: TitleMenu
 * @Description: 顶部菜单
 * @author: City Jiangsy@500wan.com
 * @date: 2011-11-29 上午10:09:19
 * 
 */
public class TitleMenu implements OnClickListener {
	private Context context;
	private View view;
	private LayoutInflater layoutInflater;
	private ImageButton btnHelp;
	private TextView tvTitle;
	private RelativeLayout relativeTop;
	private LinearLayout linearExpand;
	private GridView gridMenu;
	private LinearLayout linearBottom;
	private boolean isExpand;
	private OnClickListener btnHelpListener;
	private TitleMenuGridAdapter titleMenuGridAdapter;
	private RelativeLayout relativeBg;

	/**
	 * <p>
	 * Title:
	 * </p>
	 * <p>
	 * Description:请注意updateListContent()方法,先进行此操作才可使用getView()方法
	 * </p>
	 * 
	 * @param context
	 * @param btnHelpListener
	 */
	public TitleMenu(Context context, OnClickListener btnHelpListener) {
		this.context = context;
		this.layoutInflater = LayoutInflater.from(context);
		this.btnHelpListener = btnHelpListener;
		init();
	}

	public void updateListContent(ArrayList<Hashtable<String, Object>> list) {
		titleMenuGridAdapter = new TitleMenuGridAdapter(context, list);
		gridMenu.setAdapter(titleMenuGridAdapter);
	}

	public void setTitle(String titleText) {
		tvTitle = (TextView) view.findViewById(R.id.titlemenulayout_tv_title);
		tvTitle.setText(titleText);
	}

	private void init() {
		// TODO Auto-generated method stub
		view = layoutInflater.inflate(R.layout.title_menu_layout, null);
		isLock = false;
		isExpand = false;
		btnHelp = (ImageButton) view.findViewById(R.id.titlemenulayout_btn_help);
		btnHelp.setOnClickListener(btnHelpListener);
		relativeTop = (RelativeLayout) view.findViewById(R.id.titlemenulayout_relative_title);
		relativeTop.setOnClickListener(this);
		linearExpand = (LinearLayout) view.findViewById(R.id.titlemenulayout_linear_expand);
		linearBottom = (LinearLayout) view.findViewById(R.id.titlemenulayout_linear_bottom);
		linearBottom.setOnClickListener(this);
		gridMenu = (GridView) view.findViewById(R.id.titlemenulayout_grid_menu);
		linearExpand.setVisibility(View.GONE);
		relativeBg = (RelativeLayout) view.findViewById(R.id.titlemenulayout_relative_bg);
		relativeBg.setOnClickListener(this);
		relativeBg.setVisibility(View.GONE);
		updateRelativeTopBg();
	}

	private void updateRelativeTopBg() {
		if (isExpand) {
			relativeTop.setBackgroundResource(R.drawable.titlebar_default_bar_titlebar_320x40);
		} else {
			relativeTop.setBackgroundResource(R.drawable.title_menu_style);
		}
	}

	public View getView() {
		return view;
	}

	public void hideMenu() {
		// linearExpand.setAnimation(AnimationUtils.loadAnimation(context,
		// R.anim.push_up_out));
		linearExpand.setVisibility(View.GONE);
		relativeBg.setVisibility(View.GONE);
		isExpand = false;
		updateRelativeTopBg();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.titlemenulayout_relative_title:
			if (!isLock) {
				batterLocker();
				if (isExpand) {
					hideMenu();
				} else {
					linearExpand.setVisibility(View.VISIBLE);
					linearExpand.setAnimation(AnimationUtils.loadAnimation(context, R.anim.push_down_in));
					relativeBg.setVisibility(View.VISIBLE);
					isExpand = true;
				}
				updateRelativeTopBg();
			}
			break;

		case R.id.titlemenulayout_linear_bottom:
			if (!isLock) {
				batterLocker();
				hideMenu();
				updateRelativeTopBg();
			}
			break;

		case R.id.titlemenulayout_relative_bg:
			hideMenu();
			break;

		default:
			break;
		}

	}

	private boolean isLock;

	private void batterLocker() {
		isLock = true;
		new Thread() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				try {
					sleep(600);
					isLock = false;
				} catch (Exception e) {
					// TODO: handle exception
				}
			}

		}.start();
	}
}
